package dl.demo.controller;

import dl.demo.entity.Project;
import dl.demo.entity.User;
import dl.demo.repository.ProjectRepo;
import dl.demo.repository.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/projects")
public class ProjectListCtrl {
    @Autowired
    ProjectRepo projectRepo;
    @Autowired
    UserRepo userRepo;

    @GetMapping("/all")
    List<Project.Project_p> all(){
        Sort sort = Sort.by(Sort.Direction.DESC,"folks");
        List<Project.Project_p> projects = projectRepo.findAll();
        System.out.println(projects);
        return projects;
    }

    @GetMapping("/example")
    List<Project> example(){
        return projectRepo.getProjectExamples();
    }

    @GetMapping("/user")
    List<Project> user(){
        User user = userRepo.findUserByName("admin");
        System.out.println(user.getId());
        return projectRepo.getProjectsByUser(user.getId().toString());
    }

    @GetMapping("/star")
    List<Project> star(){
        User user = userRepo.findUserByName("ddd");
        System.out.println(user.getId());
        return projectRepo.getProjectsByStar(user.getId());
    }
}
